﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Service.Jihuabu;
using Enterprise.Model.Jihuabu;
using Enterprise.Infrastructure;
using System.Data;

namespace Enterprise.UI.Web.Modules.Jihuabu
{
    public partial class M01_Js : System.Web.UI.Page
    {
        //统计开始时间
        string s1 = (string)Utility.sink("s1", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        //统计结束时间
        string s2 = (string)Utility.sink("s2", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        //统计区间 0,50,80,100
        string splitstr = (string)Utility.sink("splitstr", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);

        string type = (string)Utility.sink("type", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindGrid();
            }
        }

        private void bindGrid()
        {

            string sql = "";

            #region "按区间统计个数"
            if (type == "1")
            {

                string outsql = "select NF,";

                string[] cols = splitstr.Split(',');

                for (int i = 0; i < cols.Length; i++)
                {
                    if (i + 1 < cols.Length)
                        outsql += "sum(case when wcl>=" + cols[i] + " and wcl<" + cols[i + 1] + " then 1 else 0 end) as c" + cols[i] + "to" + cols[i + 1] + ",";
                    else
                        outsql += "sum(case when wcl>=" + cols[i] + " then 1 else 0 end) as c" + cols[i] + ",";
                }

                outsql = outsql.Remove(outsql.Length - 1);

                string view = @"select x2.NF,nvl(X2.WCL,0) as wcl from 
(
select afebh,max(s.wcsj) as wcsj from 
(
select  substr(b.kgsj,0,4) as nf,a.afebh,b.xmfl,b.xmmc,b.xmssgs,b.afepz_my,b.afepz_jg,b.shiji_jg,b.zt,a.wcsj,a.wc_jg,b.kgsj,round(a.wc_jg/b.afepz_jg,4)*100 as wcl from jhb_m01_jdb a ,jhb_m01_xmb b where  a.afebh=b.afebh(+)
) s group by afebh
) x1
left join 

(select  substr(b.kgsj,0,4) as nf,a.afebh,b.xmfl,b.xmmc,b.xmssgs,b.afepz_my,b.afepz_jg,b.shiji_jg,b.zt,a.wcsj,a.wc_jg,b.kgsj,round(a.wc_jg/b.afepz_jg,4)*100 as wcl from jhb_m01_jdb a ,jhb_m01_xmb b where  a.afebh=b.afebh(+)
) x2

on x1.afebh=x2.afebh and x1.wcsj = x2.wcsj";

                sql = outsql + " from (" + view + " ) group by nf";
            }
            #endregion

            #region "按金额统计"
            if (type == "2")
            {
                string outsql = "select NF,sum(afepz_jg) as  AFE批复金额 ,sum(wc_jg) as 实际完成 ,round(sum(wc_jg)/sum(afepz_jg)*100,2) as 完成率";

                //string[] cols = splitstr.Split(',');

                //for (int i = 0; i < cols.Length; i++)
                //{
                //    if (i + 1 < cols.Length)
                //        outsql += "sum(wc_jg) as c" + cols[i] + "to" + cols[i + 1] + ",";
                //    else
                //        outsql += "sum(case when wcl>=" + cols[i] + " then wc_jg else 0 end) as c" + cols[i] + ",";
                //}

                // outsql = outsql.Remove(outsql.Length - 1);

                string view = @"select x2.NF,nvl(X2.WCL,0) as wcl ,x2.wc_jg,x2.afepz_jg from 
(
select afebh,max(s.wcsj) as wcsj from 
(
select  substr(b.kgsj,0,4) as nf,a.afebh,b.xmfl,b.xmmc,b.xmssgs,b.afepz_my,b.afepz_jg,b.shiji_jg,b.zt,a.wcsj,a.wc_jg,b.kgsj,round(a.wc_jg/b.afepz_jg,4)*100 as wcl from jhb_m01_jdb a ,jhb_m01_xmb b where  a.afebh=b.afebh(+)
) s group by afebh
) x1
left join 

(select  substr(b.kgsj,0,4) as nf,a.afebh,b.xmfl,b.xmmc,b.xmssgs,b.afepz_my,b.afepz_jg,b.shiji_jg,b.zt,a.wcsj,a.wc_jg,b.kgsj,round(a.wc_jg/b.afepz_jg,4)*100 as wcl from jhb_m01_jdb a ,jhb_m01_xmb b where  a.afebh=b.afebh(+)
) x2

on x1.afebh=x2.afebh and x1.wcsj = x2.wcsj";

                sql = outsql + " from (" + view + " ) group by nf";
            }
            #endregion

            Enterprise.Service.Jihuabu.M01JDBService service = new M01JDBService();
            DataSet ds = service.GetListbySQL(sql);
            Response.Write("<script type=\"text/javascript\" src=\"/Scripts/jquery-1.7.2.min.js\"></script>");
            Response.Write("<link rel=\"stylesheet\" type=\"text/css\" href=\"../../Styles/css/gridview.css\" />");
            Response.Write(ds.Tables[0].ToHtmlTable());
            if (type == "1")
            {
                Response.Write("<script type=\"text/javascript\" src=\"/Scripts/afe1.js\"></script> ");
            }
            if (type == "2")
            {
                Response.Write("<script type=\"text/javascript\" src=\"/Scripts/afe2.js\"></script>");
            }
            Response.End();
        }
    }
}